<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>布噜猫粮会员登录</h2>
      <el-form :model="form" label-width="80px" @keyup.enter="onSubmit">
        <el-form-item label="用户名">
          <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="margin-right: 10px;">登录</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';

export default {
  setup() {
    const router = useRouter();
    const userStore = useUserStore();
    const form = ref({
      userName: 'admin',
      password: 'admin'
    });

    const onSubmit = async () => {
      if (!form.value.userName || !form.value.password) {
        ElMessage.error('请输入用户名和密码');
        return;
      }

      try {
        const response = await fetch('http://localhost:8100/login/aaa', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            userName: form.value.userName,
            password: form.value.password
          })
        });

        if (!response.ok) {
          throw new Error('网络响应错误');
        }

        const data = await response.json();
        
        if (data.code === 200) {
          ElMessage.success('登录成功')
          // 在登录成功后调用
          // 存储token
          userStore.login(form.value.userName,data.data)
          // 获取token
          const token1 = userStore.token 
          // 打印存储token(验证是否存储成功)
          console.log('Current token:', token1)
          router.push('/HomePage');
        } else {
          ElMessage.error(data.msg || '登录失败');
        }
      } catch (err) {
        ElMessage.error('网络错误，请稍后重试');
      }
    };

    const onReset = () => {
      form.value.userName = 'admin';
      form.value.password = 'admin';
    };

    return {
      form,
      onSubmit,
      onReset
    };
  }
};
</script>

<style scoped>
.login-container {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.login-card {
  width: 420px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textGlow 2s ease-in-out infinite alternate;
}

@keyframes textGlow {
  from {
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
  }
  to {
    text-shadow: 2px 2px 8px rgba(102, 126, 234, 0.5);
  }
}

.el-form-item__label {
  color: #34495e;
  font-weight: 500;
}

.el-input__inner {
  border-radius: 6px;
  padding: 12px 15px;
}

.el-button {
  width: 100%;
  margin-top: 10px;
  border-radius: 6px;
  padding: 12px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  width: auto;
}

.el-form-item {
  display: flex;
  justify-content: space-between;
}

.el-form-item .el-button {
  flex: 1;
  margin-right: 10px;
}

.el-form-item .el-button:last-child {
  margin-right: 0;
}

.el-button--primary {
  background-color: #667eea;
  border-color: #667eea;
}

.el-button--primary:hover {
  background-color: #5a6fd1;
  border-color: #5a6fd1;
}

.el-button:not(.el-button--primary) {
  background-color: transparent;
  border-color: #667eea;
  color: #667eea;
}

.el-button:not(.el-button--primary):hover {
  background-color: rgba(102, 126, 234, 0.1);
}
</style>